package com.noassertions.utils;

import com.noassertions.domain.Fit;
import com.noassertions.domain.Rider;

public class BeanUtil {

	/**
	 * Accepts rider bean and returns a rider bean and a range. Subtracts percentage
	 * specified in range from each value in rider bean and returns this (lower bound) bean
	 * @param rider
	 * @param range should be a percentage. e.g. .025 for 2.5%, .10 for 10%,  etc.
	 * @return
	 */
	public static Rider getLowerRange(Rider rider, double range) {
		//TODO we might want to factor out the number formatting business and make the view responsible for formatting issues
		Rider lowerBoundRider = new Rider();
		
		if ((rider.getFemur() - rider.getFemur() * range) > 0) {
			lowerBoundRider.setFemur((float) (rider.getFemur() - rider.getFemur() * range));
			lowerBoundRider.setFemur((float) (Math.round(lowerBoundRider.getFemur()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setFemur(0);
		}

		if ((rider.getFootlength() - rider.getFootlength() * range) > 0) {
			lowerBoundRider.setFootlength((float) (rider.getFootlength() - rider.getFootlength() * range));
			lowerBoundRider.setFootlength((float) (Math.round(lowerBoundRider.getFootlength()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setFootlength(0);
		}
		
		if ((rider.getForearm() - rider.getForearm() * range) > 0) {
			lowerBoundRider.setForearm((float) (rider.getForearm() - rider.getForearm() * range));
			lowerBoundRider.setForearm((float) (Math.round(lowerBoundRider.getForearm()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setForearm(0);
		}
		
		if ((rider.getHumerus() - rider.getHumerus() * range) > 0) {
			lowerBoundRider.setHumerus((float) (rider.getHumerus() - rider.getHumerus() * range));
			lowerBoundRider.setHumerus((float) (Math.round(lowerBoundRider.getHumerus()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setHumerus(0);
		}
		
		if ((rider.getInseam() - rider.getInseam() * range) > 0) {
			lowerBoundRider.setInseam((float) (rider.getInseam() - rider.getInseam() * range));
			lowerBoundRider.setInseam((float) (Math.round(lowerBoundRider.getInseam()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setInseam(0);
		}
		
		if ((rider.getShoulderwidth() - rider.getShoulderwidth() * range) > 0) {
			lowerBoundRider.setShoulderwidth((float) (rider.getShoulderwidth() - rider.getShoulderwidth() * range));
			lowerBoundRider.setShoulderwidth((float) (Math.round(lowerBoundRider.getShoulderwidth()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setShoulderwidth(0);
		}
		
		if ((rider.getSternalnotch() - rider.getSternalnotch() * range) > 0) {
			lowerBoundRider.setSternalnotch((float) (rider.getSternalnotch() - rider.getSternalnotch() * range));
			lowerBoundRider.setSternalnotch((float) (Math.round(lowerBoundRider.getSternalnotch()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setSternalnotch(0);
		}
		
		if ((rider.getTibia() - rider.getTibia() * range) > 0) {
			lowerBoundRider.setTibia((float) (rider.getTibia() - rider.getTibia() * range));
			lowerBoundRider.setTibia((float) (Math.round(lowerBoundRider.getTibia()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setTibia(0);
		}
		
		if ((rider.getTotalarm() - rider.getTotalarm() * range) > 0) {
			lowerBoundRider.setTotalarm((float) (rider.getTotalarm() - rider.getTotalarm() * range));
			lowerBoundRider.setTotalarm((float) (Math.round(lowerBoundRider.getTotalarm()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setTotalarm(0);
		}
		
		if ((rider.getTotalbodyheight() - rider.getTotalbodyheight() * range) > 0) {
			lowerBoundRider.setTotalbodyheight((float) (rider.getTotalbodyheight() - rider.getTotalbodyheight() * range));
			lowerBoundRider.setTotalbodyheight((float) (Math.round(lowerBoundRider.getTotalbodyheight()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setTotalbodyheight(0);
		}
		
		if ((rider.getTrunk() - rider.getTrunk() * range) > 0) {
			lowerBoundRider.setTrunk((float) (rider.getTrunk() - rider.getTrunk() * range));
			lowerBoundRider.setTrunk((float) (Math.round(lowerBoundRider.getTrunk()*100.0f)/100.0f));
		} else {
			lowerBoundRider.setTrunk(0);
		}
		
		return lowerBoundRider;
	}
	
	/**
	 * Accepts rider bean and returns a rider bean and a range. Adds percentage
	 * specified in range from each value in rider bean and returns this (upper bound) bean
	 * @param rider
	 * @param range should be a percentage. e.g. .025 for 2.5%, .10 for 10%,  etc.
	 * @return
	 */
	public static Rider getUpperRange(Rider rider, float range) {
						
		Rider upperBoundRider = new Rider();
		
		if ((rider.getFemur() + rider.getFemur() * range) > 0) {
			upperBoundRider.setFemur((rider.getFemur() + rider.getFemur() * range));
			upperBoundRider.setFemur((float) (Math.round(upperBoundRider.getFemur()*100.0f)/100.0f));

		} else {
			upperBoundRider.setFemur(0);
		}

		if ((rider.getFootlength() + rider.getFootlength() * range) > 0) {
			upperBoundRider.setFootlength((float) (rider.getFootlength() + rider.getFootlength() * range));
			upperBoundRider.setFootlength((float) (Math.round(upperBoundRider.getFootlength()*100.0f)/100.0f));
		} else {
			upperBoundRider.setFootlength(0);
		}
		
		if ((rider.getForearm() + rider.getForearm() * range) > 0) {
			upperBoundRider.setForearm((float) (rider.getForearm() + rider.getForearm() * range));
			upperBoundRider.setForearm((float) (Math.round(upperBoundRider.getForearm()*100.0f)/100.0f));
		} else {
			upperBoundRider.setForearm(0);
		}
		
		if ((rider.getHumerus() + rider.getHumerus() * range) > 0) {
			upperBoundRider.setHumerus((float) (rider.getHumerus() + rider.getHumerus() * range));
			upperBoundRider.setHumerus((float) (Math.round(upperBoundRider.getHumerus()*100.0f)/100.0f));
		} else {
			upperBoundRider.setHumerus(0);
		}
		
		if ((rider.getInseam() + rider.getInseam() * range) > 0) {
			upperBoundRider.setInseam((float) (rider.getInseam() + rider.getInseam() * range));
			upperBoundRider.setInseam((float) (Math.round(upperBoundRider.getInseam()*100.0f)/100.0f));
		} else {
			upperBoundRider.setInseam(0);
		}
		
		if ((rider.getShoulderwidth() + rider.getShoulderwidth() * range) > 0) {
			upperBoundRider.setShoulderwidth((float) (rider.getShoulderwidth() + rider.getShoulderwidth() * range));
			upperBoundRider.setShoulderwidth((float) (Math.round(upperBoundRider.getShoulderwidth()*100.0f)/100.0f));
		} else {
			upperBoundRider.setShoulderwidth(0);
		}
		
		if ((rider.getSternalnotch() + rider.getSternalnotch() * range) > 0) {
			upperBoundRider.setSternalnotch((float) (rider.getSternalnotch() + rider.getSternalnotch() * range));
			upperBoundRider.setSternalnotch((float) (Math.round(upperBoundRider.getSternalnotch()*100.0f)/100.0f));
		} else {
			upperBoundRider.setSternalnotch(0);
		}
		
		if ((rider.getTibia() + rider.getTibia() * range) > 0) {
			upperBoundRider.setTibia((float) (rider.getTibia() + rider.getTibia() * range));
			upperBoundRider.setTibia((float) (Math.round(upperBoundRider.getTibia()*100.0f)/100.0f));
		} else {
			upperBoundRider.setTibia(0);
		}
		
		if ((rider.getTotalarm() + rider.getTotalarm() * range) > 0) {
			upperBoundRider.setTotalarm((float) (rider.getTotalarm() + rider.getTotalarm() * range));
			upperBoundRider.setTotalarm((float) (Math.round(upperBoundRider.getTotalarm()*100.0f)/100.0f));
		} else {
			upperBoundRider.setTotalarm(0);
		}
		
		if ((rider.getTotalbodyheight() + rider.getTotalbodyheight() * range) > 0) {
			upperBoundRider.setTotalbodyheight((float) (rider.getTotalbodyheight() + rider.getTotalbodyheight() * range));
			upperBoundRider.setTotalbodyheight((float) (Math.round(upperBoundRider.getTotalbodyheight()*100.0f)/100.0f));
		} else {
			upperBoundRider.setTotalbodyheight(0);
		}
		
		if ((rider.getTrunk() + rider.getTrunk() * range) > 0) {
			upperBoundRider.setTrunk((float) (rider.getTrunk() + rider.getTrunk() * range));
			upperBoundRider.setTrunk((float) (Math.round(upperBoundRider.getTrunk()*100.0f)/100.0f));
		} else {
			upperBoundRider.setTrunk(0);
		}
		
		return upperBoundRider;
	}
	
	/**
	 * Accepts fit bean and returns a fit bean and a range. Subtracts percentage
	 * specified in range from each value in fit bean and returns this (lower bound) bean
	 * @param fit
	 * @param range should be a percentage. e.g. .025 for 2.5%, .10 for 10%,  etc.
	 * @return
	 */
	public static Fit getLowerRange(Fit fit, double range) {
		//TODO we might want to factor out the number formatting business and make the view responsible for formatting issues
		Fit lowerBoundFit = new Fit();
		
		if ((fit.getBarswidth() - fit.getBarswidth() * range) > 0) {
			lowerBoundFit.setBarswidth((float) (fit.getBarswidth() - fit.getBarswidth() * range));
			lowerBoundFit.setBarswidth((float) (Math.round(lowerBoundFit.getBarswidth()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setBarswidth(0);
		}

		if ((fit.getCentertobars() - fit.getCentertobars() * range) > 0) {
			lowerBoundFit.setCentertobars((float) (fit.getCentertobars() - fit.getCentertobars() * range));
			lowerBoundFit.setCentertobars((float) (Math.round(lowerBoundFit.getCentertobars()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setCentertobars(0);
		}
		
		if ((fit.getCentertosaddle() - fit.getCentertosaddle() * range) > 0) {
			lowerBoundFit.setCentertosaddle((float) (fit.getCentertosaddle() - fit.getCentertosaddle() * range));
			lowerBoundFit.setCentertosaddle((float) (Math.round(lowerBoundFit.getCentertosaddle()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setCentertosaddle(0);
		}
		
		if ((fit.getFloortobars() - fit.getFloortobars() * range) > 0) {
			lowerBoundFit.setFloortobars((float) (fit.getFloortobars() - fit.getFloortobars() * range));
			lowerBoundFit.setFloortobars((float) (Math.round(lowerBoundFit.getFloortobars()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setFloortobars(0);
		}
		
		if ((fit.getFloortosaddle() - fit.getFloortosaddle() * range) > 0) {
			lowerBoundFit.setFloortosaddle((float) (fit.getFloortosaddle() - fit.getFloortosaddle() * range));
			lowerBoundFit.setFloortosaddle((float) (Math.round(lowerBoundFit.getFloortosaddle()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setFloortosaddle(0);
		}
		
		if ((fit.getSaddleheight() - fit.getSaddleheight() * range) > 0) {
			lowerBoundFit.setSaddleheight((float) (fit.getSaddleheight() - fit.getSaddleheight() * range));
			lowerBoundFit.setSaddleheight((float) (Math.round(lowerBoundFit.getSaddleheight()*100.0f)/100.0f));
		} else {
			lowerBoundFit.setSaddleheight(0);
		}
		
		return lowerBoundFit;
	}
	
	/**
	 * Accepts fit bean and returns a fit bean and a range. Adds percentage
	 * specified in range to each value in fit bean and returns this (upper bound) bean
	 * @param fit
	 * @param range should be a percentage. e.g. .025 for 2.5%, .10 for 10%, etc.
	 * @return
	 */
	public static Fit getUpperRange(Fit fit, double range) {
		//TODO we might want to factor out the number formatting business and make the view responsible for formatting issues
		Fit upperBoundFit = new Fit();
		
		if ((fit.getBarswidth() + fit.getBarswidth() * range) > 0) {
			upperBoundFit.setBarswidth((float) (fit.getBarswidth() + fit.getBarswidth() * range));
			upperBoundFit.setBarswidth((float) (Math.round(upperBoundFit.getBarswidth()*100.0f)/100.0f));
		} else {
			upperBoundFit.setBarswidth(0);
		}

		if ((fit.getCentertobars() + fit.getCentertobars() * range) > 0) {
			upperBoundFit.setCentertobars((float) (fit.getCentertobars() + fit.getCentertobars() * range));
			upperBoundFit.setCentertobars((float) (Math.round(upperBoundFit.getCentertobars()*100.0f)/100.0f));
		} else {
			upperBoundFit.setCentertobars(0);
		}
		
		if ((fit.getCentertosaddle() + fit.getCentertosaddle() * range) > 0) {
			upperBoundFit.setCentertosaddle((float) (fit.getCentertosaddle() + fit.getCentertosaddle() * range));
			upperBoundFit.setCentertosaddle((float) (Math.round(upperBoundFit.getCentertosaddle()*100.0f)/100.0f));
		} else {
			upperBoundFit.setCentertosaddle(0);
		}
		
		if ((fit.getFloortobars() + fit.getFloortobars() * range) > 0) {
			upperBoundFit.setFloortobars((float) (fit.getFloortobars() + fit.getFloortobars() * range));
			upperBoundFit.setFloortobars((float) (Math.round(upperBoundFit.getFloortobars()*100.0f)/100.0f));
		} else {
			upperBoundFit.setFloortobars(0);
		}
		
		if ((fit.getFloortosaddle() + fit.getFloortosaddle() * range) > 0) {
			upperBoundFit.setFloortosaddle((float) (fit.getFloortosaddle() + fit.getFloortosaddle() * range));
			upperBoundFit.setFloortosaddle((float) (Math.round(upperBoundFit.getFloortosaddle()*100.0f)/100.0f));
		} else {
			upperBoundFit.setFloortosaddle(0);
		}
		
		if ((fit.getSaddleheight() + fit.getSaddleheight() * range) > 0) {
			upperBoundFit.setSaddleheight((float) (fit.getSaddleheight() + fit.getSaddleheight() * range));
			upperBoundFit.setSaddleheight((float) (Math.round(upperBoundFit.getSaddleheight()*100.0f)/100.0f));
		} else {
			upperBoundFit.setSaddleheight(0);
		}
		
		return upperBoundFit;
	}
}
